// created by noellynch
// Aug 15, 2010

package com.sceneworker.wizards;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

import com.jme.util.export.Savable;
import com.jme.util.export.binary.BinaryExporter;
import com.jme2utils.staticutils.ModelUtils;
import com.jme2utils.staticutils.ModelUtils.IImportFile;
import com.myjavautils.GenericUtils;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SimpleAlert;
import com.myjavautils.swing.SwingComponentFactory;

public class ConvertModels extends WizardBase {
	private		File			mcl_loadFromFile;
	private		JLabel		mcl_loadFrom;
	
	private		File			mcl_saveToFile;
	private		JLabel		mcl_saveTo;
	
	public ConvertModels(JDialog par) {
		super(par);
	}

	@Override
	public void doWizardUI(Object clTreeParent) {
		MigLayoutUtils.asMigLayout(this);
		
		JButton		lcl_loadDir = SwingComponentFactory.makeAButton("Load From", new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mcl_loadFromFile = GenericUtils.getAFile(null, true, false);
				if(mcl_loadFromFile == null) {
					mcl_loadFrom.setText("No directory selected...");
				} else {
					mcl_loadFrom.setText(mcl_loadFromFile.getName());
				}
			}
		});
		add(lcl_loadDir);
		mcl_loadFrom = SwingComponentFactory.makeALabel("");
		add(mcl_loadFrom, "wrap");
		
		JButton		lcl_saveDir = SwingComponentFactory.makeAButton("Save To", new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mcl_saveToFile = GenericUtils.getAFile(null, true, false);
				if(mcl_saveToFile == null) {
					mcl_saveTo.setText("No directory selected...");
				} else {
					mcl_saveTo.setText(mcl_saveToFile.getName());
				}
			}
		});
		add(lcl_saveDir);
		mcl_saveTo = SwingComponentFactory.makeALabel("");
		add(mcl_saveTo, "wrap");
	}

	@Override
	public void executeWizard(Object clTreeParent) {
		if(mcl_loadFromFile == null) {
			SimpleAlert.doAlert("You have not selected a folder to load the models from.", this);
			return;
		}
		
		if(mcl_saveToFile == null) {
			SimpleAlert.doAlert("You have not selected a folder to save the models to.", this);
			return;
		}
		
		for(File lcl_f : mcl_loadFromFile.listFiles()) {
			String		lstr_ext = GenericUtils.getFileExtension(lcl_f);
			IImportFile	lcl_importer = ModelUtils.getImporter(lstr_ext);
			
			if(lcl_importer != null) {
				try {
					Object		lcl_o = lcl_importer.importFile(lcl_f.toURI().toURL());
					
					if(lcl_o instanceof Savable) {
						Savable		lcl_s = (Savable)lcl_o;
						
						File		lcl_newFile = new File(mcl_saveToFile, lcl_f.getName().replace(lstr_ext, "jme"));
						if(lcl_newFile.exists()) {
							lcl_newFile.delete();
						}
						
						BinaryExporter.getInstance().save(lcl_s, lcl_newFile);
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		}
	}

}
